import tkinter as tk
from tkinter import Text

def highlight_text():
    # 获取用户输入的高亮区域的起始和结束索引
    start_index = entry_start.get()
    end_index = entry_end.get()
    
    try:
        # 将输入的索引转换为文本的行和列坐标
        start_line, start_col = map(int, start_index.split('.'))
        end_line, end_col = map(int, end_index.split('.'))
        
        # 应用高亮
        text_widget.tag_add("highlight", start_index, end_index)
        text_widget.tag_config("highlight", background="yellow")
    except ValueError:
        pass



# 创建主窗口
root = tk.Tk()
root.title("文本编辑器")

# 创建文本编辑区域
text_widget = Text(root)
text_widget.pack()

# 创建标签和输入框来指定高亮区域的起始和结束索引
label_start = tk.Label(root, text="起始索引 (行.列):")
label_start.pack()
entry_start = tk.Entry(root)
entry_start.pack()

label_end = tk.Label(root, text="结束索引 (行.列):")
label_end.pack()
entry_end = tk.Entry(root)
entry_end.pack()

# 创建按钮以应用高亮
highlight_button = tk.Button(root, text="高亮选定文本", command=highlight_text)
highlight_button.pack()

# 运行主循环
root.mainloop()
